﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Typelib;
using Yule8xModel;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Agent
{
    public partial class UserIndex : System.Web.UI.Page
    {
        protected WxUserInfoResult wxUser;
        protected int user_id;
        protected int isShowQrCode;
        protected bool isDrawFreeDiamond;
        protected void Page_Load(object sender, EventArgs e)
        {
            // 推广代理ID
            int referral_id = RequestUtil.GetInteger("referral_id");

            AppInfo.IsDebugMode = RuntimeDataUtil.CheckDebugModel();
            // 测试用户ID
            user_id = RequestUtil.GetInteger("user_id");
            if (AppInfo.IsDebugMode && user_id > 0)
            {
                wxUser = UserModels.GetWxUserInfo(user_id);
                if (null == wxUser)
                {
                    ResponseUtil.WriteEnd("user_id无效!");
                }
            }
            else
            {
                wxUser = LoginPageBase.GetLoginInfo(false);
                if (null == wxUser)
                {
                    Response.Redirect(WechatUrls.GetUserInfoUrl("userindex", referral_id), true);
                }
            }
            if (string.IsNullOrEmpty(wxUser.nickname))
            {
                wxUser = UserModels.GetWxUserInfo(wxUser.user_id);
            }
            
            LogUtil.Instance.WriteInfo(string.Format("[用户首页] {0}", null == wxUser ? "wxUser is null" : JSON.stringify(wxUser)));
            // 是否显示二维码弹窗
            isShowQrCode = UserModels.CheckSubscribeWechat(wxUser.user_id) ? 0 : 1;
            isDrawFreeDiamond = UserModels.CheckUserDailyDrawDimondStatus(wxUser.user_id);
            /*
            object loginData = Session["login"];
            if (null != loginData)
            {
                wxUser = loginData as WxUserInfoResult;
            }
            if (null == loginData || null == wxUser)
            {
                LogUtil.Instance.WriteInfo("[普通玩家首页] 未登录");
                Response.Redirect(WechatUrls.GetUserInfoUrl("agent"), true);
            }
            LogUtil.Instance.WriteInfo("[普通玩家首页] 已登录");
            */

            string error;
            UserModels.GetGameID(wxUser.user_id, wxUser.unionid, "bozhong", out error);
            UserModels.GetGameID(wxUser.user_id, wxUser.unionid, "wanghu", out error);
            if (!AppInfo.IsDebugMode)
            {
                bool isAgent = UserModels.CheckUserIsAgent(wxUser.user_id);
                if (isAgent)
                {
                    Response.Redirect("Index.aspx?user_id=" + user_id, true); // 如果是代理则跳转到代理用户首页
                }
            }
        }
    }
}